Skip to content

Multi-language Support

Introduction

DataFaker leverages faker.js to provide localized data in over 70 languages. When using a specific language environment, the generated data will be in the corresponding language.

Supported Languages

Language CodeLanguage NameFaker Object Name
af_ZAAfrikaans (South Africa)fakerAF_ZA
arArabicfakerAR
azAzerbaijanifakerAZ
baseBasefakerBASE
bn_BDBengali (Bangladesh)fakerBN_BD
cs_CZCzech (Czech Republic)fakerCS_CZ
cyWelshfakerCY
daDanishfakerDA
deGermanfakerDE
de_ATGerman (Austria)fakerDE_AT
de_CHGerman (Switzerland)fakerDE_CH
dvDhivehifakerDV
elGreekfakerEL
enEnglishfakerEN
en_AUEnglish (Australia)fakerEN_AU
en_AU_ockerEnglish (Australian Ocker)fakerEN_AU_ocker
en_BORKEnglish (Bork)fakerEN_BORK
en_CAEnglish (Canada)fakerEN_CA
en_GBEnglish (United Kingdom)fakerEN_GB
en_GHEnglish (Ghana)fakerEN_GH
en_HKEnglish (Hong Kong)fakerEN_HK
en_IEEnglish (Ireland)fakerEN_IE
en_INEnglish (India)fakerEN_IN
en_NGEnglish (Nigeria)fakerEN_NG
en_USEnglish (United States)fakerEN_US
en_ZAEnglish (South Africa)fakerEN_ZA
eoEsperantofakerEO
esSpanishfakerES
es_MXSpanish (Mexico)fakerES_MX
faPersianfakerFA
fiFinnishfakerFI
frFrenchfakerFR
fr_BEFrench (Belgium)fakerFR_BE
fr_CAFrench (Canada)fakerFR_CA
fr_CHFrench (Switzerland)fakerFR_CH
fr_LUFrench (Luxembourg)fakerFR_LU
fr_SNFrench (Senegal)fakerFR_SN
heHebrewfakerHE
hrCroatianfakerHR
huHungarianfakerHU
hyArmenianfakerHY
id_IDIndonesian (Indonesia)fakerID_ID
itItalianfakerIT
jaJapanesefakerJA
ka_GEGeorgian (Georgia)fakerKA_GE
koKoreanfakerKO
lvLatvianfakerLV
mkMacedonianfakerMK
nb_NONorwegian (Norway)fakerNB_NO
neNepalifakerNE
nlDutchfakerNL
nl_BEDutch (Belgium)fakerNL_BE
plPolishfakerPL
pt_BRPortuguese (Brazil)fakerPT_BR
pt_PTPortuguese (Portugal)fakerPT_PT
roRomanianfakerRO
ro_MDRomanian (Moldova)fakerRO_MD
ruRussianfakerRU
skSlovakfakerSK
sr_RS_latinSerbian (Serbia, Latin script)fakerSR_RS_latin
svSwedishfakerSV
ta_INTamil (India)fakerTA_IN
thThaifakerTH
trTurkishfakerTR
ukUkrainianfakerUK
urUrdufakerUR
uz_UZ_latinUzbek (Uzbekistan, Latin script)fakerUZ_UZ_latin
viVietnamesefakerVI
yo_NGYoruba (Nigeria)fakerYO_NG
zh_CNChinese (China)fakerZH_CN
zh_TWChinese (Taiwan)fakerZH_TW
zu_ZAZulu (South Africa)fakerZU_ZA

Global Locale Setting

Basic Syntax

You can set the default global locale using the DataFaker.setLocale() method. This method accepts a parameter with the following types:

ts
// Set locale to Chinese using string
DataFaker.setLocale('zh_CN');
// Or set using the Chinese faker object directly, allFakers is a collection of all faker objects
DataFaker.setLocale(allFakers['zh_CN']);
ts
// Prioritize Chinese, then English if Chinese data doesn't exist, finally German
DataFaker.setLocale(['zh_CN', 'en_US', de_CH]);
ts
// Or you can customize the faker object
const customLocale: LocaleDefinition = {
  internet: {
    domainSuffix: ['test'],
  },
};
const customFaker = new Faker({
  locale: [customLocale, de_CH, de, en, base],
});
DataFaker.setLocale(customFaker);

Locale Caching

In fact, DataFaker.setLocale(['zh_CN', 'en_US', de_CH]) is equivalent to:

ts
const customFaker = new Faker({
  locale: [zh_CN, en_US, de_CH],
});
DataFaker.setLocale(customFaker);

It creates a new Faker object and caches it. The next time DataFaker.setLocale(['zh_CN', 'en_US', de_CH]) is called, the cached object will be used directly.

Usage Example

Example: For the user model and company model below, we can set the Chinese locale as the default, resulting in Chinese data:

ts
// User model
const userModel = defineModel('user', {
  id: 'number.int',
  firstName: 'person.firstName',
  secondName: 'person.lastName',
  age: ['number.int', { min: 18, max: 65 }],
  email: ctx => {
    return faker.internet.email({ firstName: ctx.firstName, lastName: ctx.secondeName });
  },
});
// Set global locale to Chinese
DataFaker.setLocale('zh_CN');
const userDatas = fakeData(userModel);
console.dir(userDatas, { depth: Infinity });
// Company model
const companyModel = defineModel('company', {
  name: 'company.name',
  address: 'location.streetAddress',
});
const companyDatas = fakeData(companyModel);
console.dir(companyDatas, { depth: Infinity });
json
// User data
{
  "id": 1040719705843601,
  "firstName": "鹭洋",
  "secondName": "薛",
  "age": 58,
  "email": "valljf_Larkin29@hotmail.com"
}
// Company data
{
  "name": "辽宁省榕融传媒无限公司",
  "address": "英巷28404号"
}

Runtime Locale Setting

Basic Syntax

Setting the locale at runtime means passing locale configuration when generating data with fakeData:

ts
const userDatas = fakeData(userModel, {
  // Specify locale
  locale: 'zh_CN',
});

The way to specify the locale above is consistent with the global locale setting:

ts
const userDatas = fakeData(userModel, {
  // Set locale to Chinese using string
  locale: 'zh_CN',
  // Or set using the Chinese faker object directly, allFakers is a collection of all faker objects
  locale: allFakers['zh_CN'],
});
ts
const userDatas = fakeData(userModel, {
  // Prioritize Chinese, then English if Chinese data doesn't exist, finally German
  locale: ['zh_CN', 'en_US', de_CH],
});
ts
// Or you can customize the faker object
const customLocale: LocaleDefinition = {
  internet: {
    domainSuffix: ['test'],
  },
};
const customFaker = new Faker({
  locale: [customLocale, de_CH, de, en, base],
});
const userDatas = fakeData(userModel, {
  // Prioritize Chinese, then English if Chinese data doesn't exist, finally German
  locale: customFaker,
});

Usage Example

Example: For the user model below, we can set the Chinese locale at runtime, resulting in Chinese data:

ts
const addressModel = defineModel('address', {
  country: 'location.country',
  city: 'location.city',
});
// User model
const userModel = defineModel('user', {
  firstName: 'person.firstName',
  secondName: 'person.lastName',
  age: ['number.int', { min: 18, max: 65 }],
  email: ctx => {
    return faker.internet.email({ firstName: ctx.firstName, lastName: ctx.secondeName });
  },
  address: { refModel: 'address', count: 1 },
  children: {
    refModel: 'user',
    deep: 3,
  },
});
const userDatas = fakeData(userModel, {
  // Specify locale
  locale: [zh_CN, 'en_AU'],
});
console.dir(userDatas, { depth: Infinity });
ts
{
  firstName: '黎昕',
  secondName: '止',
  age: 61,
  address: { country: '乍得', city: '安南市' },
  children: {
    firstName: '懿轩',
    secondName: '公冶',
    age: 53,
    address: { country: '缅甸', city: '衡口市' },
    children: {
      firstName: '立轩',
      secondName: '方',
      age: 64,
      address: { country: '沙特阿拉伯', city: '北门市' },
      children: {
        firstName: '嘉熙',
        secondName: '介',
        age: 29,
        address: { country: '尼日利亚', city: '吉阳市' },
        children: null,
        email: 'gztmft_Cummings@hotmail.com'
      },
      email: 'o97sbt_Abbott@yahoo.com'
    },
    email: 'jcvsbt88@yahoo.com'
  },
  email: 'vdak5x72@gmail.com'
}

Priority

Runtime locale configuration takes precedence over global configuration. For example, if you set both global and runtime locales, the runtime setting will override the global one. For instance, even if the global setting is English, the Chinese runtime setting will be used:

ts
// Set global locale
DataFaker.setLocale(faker);
const addressModel = defineModel('address', {
  country: 'location.country',
  city: 'location.city',
});
// User model
const userModel = defineModel('user', {
  firstName: 'person.firstName',
  secondName: 'person.lastName',
  age: ['number.int', { min: 18, max: 65 }],
  email: ctx => {
    return faker.internet.email({ firstName: ctx.firstName, lastName: ctx.secondeName });
  },
  address: { refModel: 'address', count: 1 },
  children: {
    refModel: 'user',
    deep: 3,
  },
});
// Specify locale at runtime
const userDatas = fakeData(userModel, {
  // Specify locale
  locale: [zh_CN, 'en_AU'],
});
console.dir(userDatas, { depth: Infinity });
ts
{
  firstName: '子欣',
  secondName: '申屠',
  age: 20,
  address: { country: '日本', city: '衡乡县' },
  children: {
    firstName: '晋鹏',
    secondName: '郝',
    age: 60,
    address: { country: '日本', city: '吉沙市' },
    children: {
      firstName: '智宸',
      secondName: '殳',
      age: 57,
      address: { country: '吉布提', city: '太口市' },
      children: {
        firstName: '修洁',
        secondName: '涂',
        age: 61,
        address: { country: '澳大利亚', city: '长京市' },
        children: null,
        email: 'fselj59@yahoo.com'
      },
      email: 'k8qi48.Rodriguez@yahoo.com'
    },
    email: 'k7fv9r_Nienow@yahoo.com'
  },
  email: 'i1cl5v_Kris@gmail.com'
}